package com.example.xuegengwang.xuegengwang;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.fragment.FourFragment;
import com.example.xuegengwang.xuegengwang.fragment.MainPageFragment;
import com.example.xuegengwang.xuegengwang.fragment.SecondFragment;
import com.example.xuegengwang.xuegengwang.fragment.ThirdFragment;
import com.example.xuegengwang.xuegengwang.service.UpdateService;

import common.BaseActivity;
import common.Constant;
import common.Variable;
import utils.ToastUtils;
import utils.UpdateUtils;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private FragmentManager fragmentManger;
    private FragmentTransaction ft;
    private MainPageFragment mainPageFragment;//首页fragment
    private SecondFragment secondFragment;//提问fragment
    private ThirdFragment thirdFragment;//资讯fragment
    private FourFragment fourFragment;//我的fragment
    private boolean second_had_added = false, third_had_added = false, four_had_added = false;

    myReceiver receiver;

    private int drump = -1;//通知开启这个activity时，要跳转到第几个fragment用的

    //底部控件组
    private RelativeLayout main_page_bottom_item1, main_page_bottom_item2, main_page_bottom_item3, main_page_bottom_item4;//点击布局
    private TextView main_page_main_tv, main_page_ask_tv, main_page_message_tv, main_page_me_tv;//字
    private ImageView main_page_main_ima, main_page_ask_ima, main_page_message_ima, main_page_me_ima;//图

    private int currentFragmentPage = 1;
    private int chosedColor;
    private int unChosedColor;
    String dialogId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drump = getIntent().getIntExtra("drump", -1);
        dialogId = MainActivity.this.getIntent().getStringExtra("dialogId");
        initView();
        initData();
        registerReceiver();
        initFragment();
        startUpdateService();
    }

    private void initView() {
        main_page_bottom_item1 = (RelativeLayout) findViewById(R.id.main_page_bottom_item1);
        main_page_bottom_item2 = (RelativeLayout) findViewById(R.id.main_page_bottom_item2);
        main_page_bottom_item3 = (RelativeLayout) findViewById(R.id.main_page_bottom_item3);
        main_page_bottom_item4 = (RelativeLayout) findViewById(R.id.main_page_bottom_item4);
        main_page_bottom_item1.setOnClickListener(this);
        main_page_bottom_item2.setOnClickListener(this);
        main_page_bottom_item3.setOnClickListener(this);
        main_page_bottom_item4.setOnClickListener(this);
        main_page_main_tv = (TextView) findViewById(R.id.main_page_main_tv);
        main_page_ask_tv = (TextView) findViewById(R.id.main_page_ask_tv);
        main_page_message_tv = (TextView) findViewById(R.id.main_page_message_tv);
        main_page_me_tv = (TextView) findViewById(R.id.main_page_me_tv);
        main_page_main_ima = (ImageView) findViewById(R.id.main_page_main_ima);
        main_page_ask_ima = (ImageView) findViewById(R.id.main_page_ask_ima);
        main_page_message_ima = (ImageView) findViewById(R.id.main_page_message_ima);
        main_page_me_ima = (ImageView) findViewById(R.id.main_page_me_ima);
    }

    private void initData() {
        chosedColor = MainActivity.this.getResources().getColor(R.color.common_color);
        unChosedColor = MainActivity.this.getResources().getColor(R.color.text_color);
    }

    private void initFragment() {
        fragmentManger = MainActivity.this.getSupportFragmentManager();
        ft = fragmentManger.beginTransaction();
        mainPageFragment = new MainPageFragment();
        secondFragment = new SecondFragment();
        thirdFragment = new ThirdFragment();
        fourFragment = new FourFragment();
        ft.add(R.id.main_page_fragment_layout, mainPageFragment);
        ft.commit();
        if (drump != -1) {
            changeBottom(drump);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_page_bottom_item1:
                changeBottom(1);
                break;
            case R.id.main_page_bottom_item2:
                changeBottom(2);
                break;
            case R.id.main_page_bottom_item3:
                changeBottom(3);
                break;
            case R.id.main_page_bottom_item4:
                changeBottom(4);
                break;
            default:
                break;

        }
    }

    private void changeBottom(int position) {
        if (currentFragmentPage != position) {
            ft = fragmentManger.beginTransaction();
            switch (position) {
                case 1:
                    //点了首页
                    currentFragmentPage = 1;
                    ft.hide(secondFragment);
                    ft.hide(thirdFragment);
                    ft.hide(fourFragment);
                    ft.show(mainPageFragment);
                    main_page_main_tv.setTextColor(chosedColor);
                    main_page_ask_tv.setTextColor(unChosedColor);
                    main_page_message_tv.setTextColor(unChosedColor);
                    main_page_me_tv.setTextColor(unChosedColor);
                    main_page_main_ima.setImageResource(R.mipmap.btn_main_page_chose_img);
                    main_page_ask_ima.setImageResource(R.mipmap.btn_ask_unchose_img);
                    main_page_message_ima.setImageResource(R.mipmap.btn_message_unchose_img);
                    main_page_me_ima.setImageResource(R.mipmap.btn_me_unchose_img);
                    break;
                case 2:
                    //点了提问.
                    currentFragmentPage = 2;
                    if (!second_had_added) {
                        ft.add(R.id.main_page_fragment_layout, secondFragment);
                        second_had_added = true;
                    }
                    ft.hide(mainPageFragment);
                    ft.hide(thirdFragment);
                    ft.hide(fourFragment);
                    ft.show(secondFragment);
                    main_page_main_tv.setTextColor(unChosedColor);
                    main_page_ask_tv.setTextColor(chosedColor);
                    main_page_message_tv.setTextColor(unChosedColor);
                    main_page_me_tv.setTextColor(unChosedColor);
                    main_page_main_ima.setImageResource(R.mipmap.btn_main_page_unchose_img);
                    main_page_ask_ima.setImageResource(R.mipmap.btn_ask_chose_img);
                    main_page_message_ima.setImageResource(R.mipmap.btn_message_unchose_img);
                    main_page_me_ima.setImageResource(R.mipmap.btn_me_unchose_img);
                    break;
                case 3:
                    //点了资讯
                    currentFragmentPage = 3;
                    if (!third_had_added) {
                        ft.add(R.id.main_page_fragment_layout, thirdFragment);
                        third_had_added = true;
                    }
                    ft.hide(mainPageFragment);
                    ft.hide(secondFragment);
                    ft.hide(fourFragment);
                    ft.show(thirdFragment);
                    main_page_main_tv.setTextColor(unChosedColor);
                    main_page_ask_tv.setTextColor(unChosedColor);
                    main_page_message_tv.setTextColor(chosedColor);
                    main_page_me_tv.setTextColor(unChosedColor);
                    main_page_main_ima.setImageResource(R.mipmap.btn_main_page_unchose_img);
                    main_page_ask_ima.setImageResource(R.mipmap.btn_ask_unchose_img);
                    main_page_message_ima.setImageResource(R.mipmap.btn_message_chose_img);
                    main_page_me_ima.setImageResource(R.mipmap.btn_me_unchose_img);
                    break;
                case 4:
                    //点了我的
                    currentFragmentPage = 4;
                    if (!four_had_added) {
                        ft.add(R.id.main_page_fragment_layout, fourFragment);
                        four_had_added = true;
                    }
                    ft.hide(mainPageFragment);
                    ft.hide(secondFragment);
                    ft.hide(thirdFragment);
                    ft.show(fourFragment);
                    main_page_main_tv.setTextColor(unChosedColor);
                    main_page_ask_tv.setTextColor(unChosedColor);
                    main_page_message_tv.setTextColor(unChosedColor);
                    main_page_me_tv.setTextColor(chosedColor);
                    main_page_main_ima.setImageResource(R.mipmap.btn_main_page_unchose_img);
                    main_page_ask_ima.setImageResource(R.mipmap.btn_ask_unchose_img);
                    main_page_message_ima.setImageResource(R.mipmap.btn_message_unchose_img);
                    main_page_me_ima.setImageResource(R.mipmap.btn_me_chose_img);
                    break;
                default:
                    break;
            }
            ft.commitAllowingStateLoss();
        }
    }

    int lastBackPressedTime = 0;

    @Override
    public void onBackPressed() {
        int thisTime = (int) System.currentTimeMillis();
        if (Math.abs((Math.abs(thisTime) - lastBackPressedTime)) <= 1500) {
            ToastUtils.cancelToast();
            MainActivity.this.finish();
        } else {
            lastBackPressedTime = Math.abs(thisTime);
            ToastUtils.shortToast("再点击一次退出应用");
        }
    }

    private void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACITION_FOURFRAGMENT);
        intentFilter.addAction(Constant.ACTION_DOWNLOAD_FILE);
        intentFilter.addAction(Constant.ACTION_NEW_VERSION);
        receiver = new myReceiver();
        this.registerReceiver(receiver, intentFilter);
    }

    class myReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case Constant.ACITION_FOURFRAGMENT:
                    changeBottom(4);
                    break;
                case Constant.ACTION_NEW_VERSION:
                    //有新版本
                    UpdateUtils.getInstance().updateNewVersion(MainActivity.this);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(receiver);
    }

    public void goToOnlineAsk() {
        //跳转到在线提问，即志愿者列表
        changeBottom(2);
        secondFragment.changePage(1);
    }

    private void startUpdateService(){
        //查询监听服务
        if(Variable.IS_NEW_VERSION) {
            UpdateUtils.getInstance().updateNewVersion(MainActivity.this);
        }else{
            Intent i = new Intent(this, UpdateService.class);
            startService(i);
        }
    }

}
